1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 
12 module hip.graphics.mesh;
13 import hip.hiprenderer.renderer;
14 import hip.hiprenderer.shader;
15 import hip.hiprenderer.vertex;
16 import hip.error.handler;
17 import std.traits;
18 
19 class Mesh
20 {
21     protected index_t[] indices;
22     protected void[] vertices;
23     ///Not yet supported
24     bool isInstanced;
25     private bool isBound;
26     protected HipVertexArrayObject vao;
27     Shader shader;
28     this(HipVertexArrayObject vao, Shader shader)
29     {
30         this.vao = vao;
31         this.shader = shader;
32     }
33     void createVertexBuffer(index_t count, HipResourceUsage usage)
34     {
35         this.vao.createVertexBuffer(count, usage);
36     }
37     void createIndexBuffer(index_t count, HipResourceUsage usage)
38     {
39         this.vao.createIndexBuffer(count, usage);
40     }
41     void sendAttributes()
42     {
43         this.vao.sendAttributes(shader);
44     }
45 
46     void bind()
47     {
48         if(!this.isBound)
49         {
50             this.isBound = true;
51             this.shader.bind();
52             this.vao.bind();
53         }
54         // else assert(false, "Erroneous call to bind.");
55     }
56     void unbind()
57     {
58         if(this.isBound)
59         {
60             this.isBound = false;
61             this.shader.unbind();
62             this.vao.unbind();
63         }
64         // else assert(false, "Erroneous call to unbind.");
65     }
66 
67     /**
68      * Use that function when the mesh doesn't hold ownership over the indices
69      * Params:
70      *   indexBuffer = The index buffer to be shared
71      */
72     public void setIndices(IHipRendererBuffer indexBuffer)
73     {
74         this.indices = null;
75         this.vao.setIndexBuffer(indexBuffer);
76     }
77 
78     /**
79     *   Will choose between resizing buffer as needed or only updating it.
80     */
81     public void setIndices(index_t[] indices)
82     {
83         if(indices.length <  this.indices.length)
84         {
85             updateIndices(indices);
86             return;
87         }
88         this.indices = indices;
89         this.vao.setIndices(indices);
90     }
91 
92     public void setVertices(const void[] vertices)
93     {
94         if(vertices.length <= this.vertices.length)
95         {
96             updateVertices(vertices);
97             return;
98         }
99         this.vertices = cast(void[])vertices;
100         this.vao.setVertices(vertices);
101     }
102     /**
103     *   Updates the GPU internal buffer by using the buffer sent.
104     *   The offset is always multiplied by the target vertex buffer stride.
105     */
106     public void updateVertices(const void[] vertices, int offset = 0)
107     {
108         this.vao.updateVertices(vertices, offset);
109     }
110     public void updateIndices(const index_t[] indices, int offset = 0)
111     {
112         this.vao.updateIndices(indices, offset);
113     }
114     public void setShader(Shader s){this.shader = s;}
115 
116     /**
117     *   How many indices should it draw
118     */
119     public void draw(T)(T count, HipRendererMode mode, uint offset = 0)
120     {
121         static assert(isUnsigned!T, "Mesh must receive an integral type in its draw");
122         ErrorHandler.assertExit(count < T.max, "Can't draw more than T.max");
123         // if(isVertexArray)
124         // {
125             // HipRenderer.drawVertices()
126         // }
127         //else if(isInstanced)
128         /*
129         {
130             HipRenderer.drawInstanced()
131         }
132         */
133         if(!isBound) bind();
134         HipRenderer.drawIndexed(mode, cast(index_t)count, offset);
135     }
136 
137 }